package com.long1008.controller.personnel;

import com.long1008.pojo.EmpTrain;
import com.long1008.pojo.Employee;
import com.long1008.pojo.RespBean;
import com.long1008.pojo.RespPageEntity;
import com.long1008.service.EmpService;
import com.long1008.service.PersonnelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/personnel/train")
public class EmpTrainController {

    @Autowired
    PersonnelService personnelService;

    @Autowired
    EmpService empService;

    @RequestMapping(value = "/trains", method = RequestMethod.POST)
    public RespBean addEmpTrain(EmpTrain empTrain){
        Employee employee = empService.getEmpById(empTrain.getEid());
        if(empService.getEmpById(empTrain.getEid())==null){
            return RespBean.error("员工号不存在");
        }
        if (!employee.getName().equals(empTrain.getEmpName())) {
            return RespBean.error("员工号与员工姓名不匹配！");
        }
        if(personnelService.addEmpTrain(empTrain)==1){
            return RespBean.ok("添加成功");
        }
        return RespBean.error("添加失败");
    }

    @RequestMapping(value = "/trains", method = RequestMethod.GET)
    public List<EmpTrain> getAllTrains(){
        return personnelService.getAllEmpTrains();
    }

    /**
     * 根据页码和大小分页查询
     * @param page 当前页，默认为1
     * @param size 当前每页显示行数，默认为10
     * @return 页信息的实体
     */
    @GetMapping("/trains/ts")
    public RespPageEntity getTrainsByPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        return personnelService.getTrainsByPage(page, size);
    }
    //根据员工姓名筛选培训记录
    @RequestMapping(value = "/trains/empName/{empName}")
    public RespPageEntity getTrainsByEmpName(
            @PathVariable(required = false) String empName,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size
            ){
        return personnelService.getTrainsByEmpName(empName, page, size);
    }
    //根据员工培训日期筛选培训记录
    @RequestMapping(value = "/trains/trainDate/{trainDate}")
        public RespPageEntity getTrainsByTrainDate(
                @PathVariable(required = false) Date trainDate,
                @RequestParam(defaultValue = "1") Integer page,
                @RequestParam(defaultValue = "10") Integer size){
        return personnelService.getTrainsByTrainDate(trainDate, page, size);
    }
//    @RequestMapping(value = "/trains/{eid}")
//    public List<EmpTrain> getEmpTrainsByWokrID(@PathVariable(required = false) String eid){
//        List<EmpTrain> empTrain = personnelService.getEmpTrainsByWokrID(eid);
//        return empTrain;
//    }
    @RequestMapping(value = "/trains", method = RequestMethod.PUT)
    public RespBean updateEmpTrain(EmpTrain empTrain){
        Employee employee = empService.getEmpById(empTrain.getEid());
        if(empService.getEmpById(empTrain.getEid())==null){
            return RespBean.error("员工号不存在");
        }
        if (!employee.getName().equals(empTrain.getEmpName())) {
            return RespBean.error("员工号与员工姓名不匹配！");
        }
        if(personnelService.updateEmpTrain(empTrain)==1){
            return RespBean.ok("修改成功");
        }
        return RespBean.error("修改失败");
    }
    @RequestMapping(value = "/trains/{ids}", method = RequestMethod.DELETE)
    public RespBean deleteEmpTrain(@PathVariable String ids){
        if(personnelService.deleteEmpTrain(ids)>=1){
            return RespBean.ok("删除成功");
        }
        return RespBean.error("删除失败");
    }

}
